//
//  PayInfoViewController.m
//  Haliaeetus
//
//  Created by junli hou on 2018/4/30.
//  Copyright © 2018年 BMY. All rights reserved.
//

#import "PayInfoViewController.h"

@interface PayInfoViewController ()
@property (nonatomic,strong) UILabel *cardTypeLabel;
@property (nonatomic,strong) UITextField *cardTypeTextField;
@property (nonatomic,strong) UILabel *cardNumberLabel;
@property (nonatomic,strong) UITextField *cardNumberTextField;
@property (nonatomic,strong) UILabel *expiredAtLabel;
@property (nonatomic,strong) UITextField *expiredAtTextField;
@property (nonatomic,strong) UILabel *expiredMonthLabel;
@property (nonatomic,strong) UITextField *expiredMonthTextField;
@property (nonatomic,strong) UILabel *cardNameLabel;
@property (nonatomic,strong) UITextField *cardNameTextField;


@property (nonatomic,strong) UIButton *saveBtn;
@end

@implementation PayInfoViewController
- (instancetype)initWithPayInfoSuccessBlock:(EditPayInfoSuccessBlock)successBlock
{
    self = [super init];
    if (self) {
        _successBlock = successBlock;
        
    }
    return self;
    
}
- (void)loadView {
    [super loadView];
    self.title =@"支付信息";
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self.navigationController setLeftBtnWithTarget:self action:@selector(goBack)];
    [IQKeyboardManager sharedManager].enable = YES;
    [self initView];
    
}
- (void)goBack {
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}
- (void)initView{
    
    
    
    self.cardNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(14,18,90,17)];
    self.cardNameLabel.text = @"持卡人姓名";
    self.cardNameLabel.textColor = WS_RGBA(141,141,141,1);
    self.cardNameLabel.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:self.cardNameLabel];
    
    self.cardNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(14,CGRectGetMaxY(self.cardNameLabel.frame)+6,self.view.frame.size.width - 50,20)];
    
    self.cardNameTextField.backgroundColor = [UIColor whiteColor];
    self.cardNameTextField.placeholder=@"请填写您的银行卡持卡人姓名";
    self.cardNameTextField.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:self.cardNameTextField];
    
    UIImageView *cardNameLineView = [[UIImageView alloc]initWithFrame:CGRectMake(0,79.5,self.view.frame.size.width,0.5)];
    cardNameLineView.backgroundColor = WS_RGBA(234, 234, 234, 0.5);
    [self.view addSubview:cardNameLineView];
    
    
    
    self.cardTypeLabel = [[UILabel alloc]initWithFrame:CGRectMake(14,CGRectGetMaxY(cardNameLineView.frame)+18,90,17)];
    self.cardTypeLabel.text = @"银行卡类别";
    self.cardTypeLabel.textColor = WS_RGBA(141,141,141,1);
    self.cardTypeLabel.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:self.cardTypeLabel];
    
    self.cardTypeTextField = [[UITextField alloc]initWithFrame:CGRectMake(14,CGRectGetMaxY(self.cardTypeLabel.frame)+6,self.view.frame.size.width - 50,20)];
    
    self.cardTypeTextField.backgroundColor = [UIColor whiteColor];
    self.cardTypeTextField.placeholder=@"请填写您的银行卡类别（例如：Visa/Master等）";
    self.cardTypeTextField.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:self.cardTypeTextField];
    
    UIImageView *cardTypeLineView = [[UIImageView alloc]initWithFrame:CGRectMake(0,79.5+80,self.view.frame.size.width,0.5)];
    cardTypeLineView.backgroundColor = WS_RGBA(234, 234, 234, 0.5);
    [self.view addSubview:cardTypeLineView];
    
    self.cardNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(14,CGRectGetMaxY(cardTypeLineView.frame)+18,90,17)];
    self.cardNumberLabel.text = @"卡号";
    self.cardNumberLabel.textColor = WS_RGBA(141,141,141,1);
    self.cardNumberLabel.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:self.cardNumberLabel];
    
    self.cardNumberTextField = [[UITextField alloc]initWithFrame:CGRectMake(14,CGRectGetMaxY(self.cardNumberLabel.frame)+6,self.view.frame.size.width - 50,20)];
    
    self.cardNumberTextField.backgroundColor = [UIColor whiteColor];
    self.cardNumberTextField.placeholder=@"请填写您的银行卡号";
    self.cardNumberTextField.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:self.cardNumberTextField];
    
    UIImageView *cardNumberView = [[UIImageView alloc]initWithFrame:CGRectMake(0,79.5+80+80,self.view.frame.size.width,0.5)];
    cardNumberView.backgroundColor = WS_RGBA(234, 234, 234, 0.5);
    [self.view addSubview:cardNumberView];
    
    self.expiredAtLabel = [[UILabel alloc]initWithFrame:CGRectMake(14,CGRectGetMaxY(cardNumberView.frame)+18,290,17)];
    self.expiredAtLabel.text = @"到期年份(比如：2018年填写“18”)";
    self.expiredAtLabel.textColor = WS_RGBA(141,141,141,1);
    self.expiredAtLabel.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:self.expiredAtLabel];
    
    self.expiredAtTextField = [[UITextField alloc]initWithFrame:CGRectMake(14,CGRectGetMaxY(self.expiredAtLabel.frame)+6,self.view.frame.size.width - 50,20)];
    
    self.expiredAtTextField.backgroundColor = [UIColor whiteColor];
    self.expiredAtTextField.placeholder=@"请填写您银行卡的到期年份";
    self.expiredAtTextField.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:self.expiredAtTextField];
    
    UIImageView *expiredAtLineView = [[UIImageView alloc]initWithFrame:CGRectMake(0,79.5+80+80+80,self.view.frame.size.width,0.5)];
    expiredAtLineView.backgroundColor = WS_RGBA(234, 234, 234, 0.5);
    [self.view addSubview:expiredAtLineView];
    

    self.expiredMonthLabel = [[UILabel alloc]initWithFrame:CGRectMake(14,CGRectGetMaxY(expiredAtLineView.frame)+18,290,17)];
    self.expiredMonthLabel.text = @"到期月份(比如：3月填写“03”)";
    self.expiredMonthLabel.textColor = WS_RGBA(141,141,141,1);
    self.expiredMonthLabel.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:self.expiredMonthLabel];
    
    self.expiredMonthTextField = [[UITextField alloc]initWithFrame:CGRectMake(14,CGRectGetMaxY(self.expiredMonthLabel.frame)+6,self.view.frame.size.width - 50,20)];
    
    self.expiredMonthTextField.backgroundColor = [UIColor whiteColor];
    self.expiredMonthTextField.placeholder=@"请填写您银行卡的到期月份";
    self.expiredMonthTextField.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:self.expiredMonthTextField];
    
    UIImageView *expiredMonthLineView = [[UIImageView alloc]initWithFrame:CGRectMake(0,79.5+80+80+80+80,self.view.frame.size.width,0.5)];
    expiredMonthLineView.backgroundColor = WS_RGBA(234, 234, 234, 0.5);
    [self.view addSubview:expiredMonthLineView];
    
    
    
    
    
    
    //save
    self.saveBtn = [[UIButton alloc]init];
    self.saveBtn.frame =CGRectMake(15,CGRectGetMaxY(expiredMonthLineView.frame) + 20,WS_SCREEN_FRAME_WIDTH-30,40);
    self.saveBtn.layer.cornerRadius=2;
    self.saveBtn.layer.masksToBounds = YES;
    self.saveBtn.backgroundColor = WS_RGB(255, 134, 35);
    self.saveBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.saveBtn setTitleColor:WS_RGB(255, 255, 255) forState:UIControlStateNormal];
    [self.saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [self.saveBtn addTarget:self action:@selector(VerificationText) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.saveBtn];
    
   if ([self.payInfoDic objectForKey:@"id"]) {
     self.cardNameTextField.text=WS_STR_NOT_NULL([self.payInfoDic objectForKey:@"card_name"]);
     self.cardTypeTextField.text=WS_STR_NOT_NULL([self.payInfoDic objectForKey:@"card_type"]);
     self.cardNumberTextField.text=WS_STR_NOT_NULL([self.payInfoDic objectForKey:@"card_number"]);
     self.expiredAtTextField.text=WS_STR_NOT_NULL([self.payInfoDic objectForKey:@"expiration_year"]);
     self.expiredMonthTextField.text=WS_STR_NOT_NULL([self.payInfoDic objectForKey:@"expiration_month"]);
        }

}
- (void)VerificationText{
    
    
    if ([self.cardTypeTextField.text isEqualToString:@""] || self.cardTypeTextField.text.length == 0) {
        WSToast(@"请输入信用卡类别")
        
    }else if([self.cardNumberTextField.text isEqualToString:@""] || self.cardNumberTextField.text.length == 0){
        WSToast(@"请输入卡号")
        
    }else if([self.expiredAtTextField.text isEqualToString:@""] || self.expiredAtTextField.text.length == 0){
        WSToast(@"请输入银行卡有效年份")
        
    }else if([self.expiredMonthTextField.text isEqualToString:@""] || self.expiredMonthTextField.text.length == 0){
        WSToast(@"请输入银行卡有效月份")
        
    }else if([self.cardNameTextField.text isEqualToString:@""] || self.cardNameTextField.text.length == 0){
        WSToast(@"请输入银行卡持卡人姓名")
        
    }else{
        
            if ([self.payInfoDic objectForKey:@"id"]) {
                [self updateCardInfo];
            }else{
                [self creatCardInfo];
            }

    }
}
-(void)creatCardInfo
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setValue:self.cardNameTextField.text forKey:@"card_name"];
    [params setValue:self.cardTypeTextField.text forKey:@"card_type"];
    [params setValue:self.cardNumberTextField.text forKey:@"card_number"];
    [params setValue:self.expiredAtTextField.text forKey:@"expiration_year"];
    [params setValue:self.expiredMonthTextField.text forKey:@"expiration_month"];
    
    
    [SVProgressHUD showWithStatus:@"loading"];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [WSNetwork WSHttpsByType:@"POST" Url:[NSString stringWithFormat:@"%@%@",BMY_URL,@"/credit_cards"] params:params onCompletion:^(NSMutableDictionary *dic){
        [SVProgressHUD dismiss];
        if ([[dic objectForKey:@"biz_action"] intValue]==0) {
            //获取成功
            WSToast(@"保存成功");
       [self performSelector:@selector(goBack) withObject:nil afterDelay:1.0];

        }else{
            //错误提示
            WSToast([dic objectForKey:@"biz_msg"]);
        }
        
    }onError:^(NSError *error){
        [SVProgressHUD dismiss];
        WSToast(@"保存失败")
    }];
    
    
}
-(void)updateCardInfo
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setValue:self.cardNameTextField.text forKey:@"card_name"];
    [params setValue:self.cardTypeTextField.text forKey:@"card_type"];
    [params setValue:self.cardNumberTextField.text forKey:@"card_number"];
    [params setValue:self.expiredAtTextField.text forKey:@"expiration_year"];
    [params setValue:self.expiredMonthTextField.text forKey:@"expiration_month"];

    
    
    [SVProgressHUD showWithStatus:@"loading"];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [WSNetwork WSHttpsByType:@"PUT" Url:[NSString stringWithFormat:@"%@%@%@",BMY_URL,@"/credit_cards/",WS_STR_NOT_NULL([self.payInfoDic objectForKey:@"id"])] params:params onCompletion:^(NSMutableDictionary *dic){
        [SVProgressHUD dismiss];
        if ([[dic objectForKey:@"biz_action"] intValue]==0) {
            //获取成功
            WSToast(@"保存成功");
            [self performSelector:@selector(goBack) withObject:nil afterDelay:1.0];
        }else{
            //错误提示
            WSToast([dic objectForKey:@"biz_msg"]);
        }
        
    }onError:^(NSError *error){
        [SVProgressHUD dismiss];
        WSToast(@"保存失败")
    }];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
